/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 12/8/2010
 * Time: 9:49 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using popLib.ExtensionClass;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace popLib.ExtensionClass
{
	/// <summary>
	/// Inherite this class to send and receive window message.
	/// </summary>
	public class HandleWindow:Form
	{
		public string file_config = FileLib.getFullPath("config.ini");
		public popLib.ExtensionClass.INISetting config;
		
		public delegate void ReceiveWindowMessage( string msg );
		public event ReceiveWindowMessage ReceiveWindowMessageEvent;
		
		public HandleWindow(){
			if( config == null ) config = new popLib.ExtensionClass.INISetting(file_config);
			config.Load();			
		}
		
		public void InitConfig(string config_path){
			file_config = config_path;
			config = new popLib.ExtensionClass.INISetting(file_config);
			config.Load();
		}
		
		#region WindowMessage Functions
		public const int WM_COPYDATA = 0x4A;
		
		public struct COPYDATASTRUCT
	    {
	        public IntPtr dwData;
	        public int cbData;
	        [MarshalAs(UnmanagedType.LPStr)]
	        public string lpData;
	    }
		
		//For use with WM_COPYDATA and COPYDATASTRUCT
		[DllImport("User32.dll", EntryPoint = "SendMessage")]
		public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
		
		public bool SendWindowMessage(string msg, string className, string windowName){
            IntPtr Client3D;
            try{
	            Process mainClientProcess = null;
	            foreach (Process p in Process.GetProcesses())
	                if (p.ProcessName.Equals(windowName))
	                {
	                    mainClientProcess = p;
	                    break;
	                }
	
	            if (mainClientProcess != null)
	                Client3D = mainClientProcess.MainWindowHandle;
	            else
	                Client3D = APILib.FindWindow(StringLib.isValid(className) ? className : null, windowName);
				
				if( Client3D != IntPtr.Zero ){
	            	//bring window on top and active it
	            	APILib.SetForegroundWindow(Client3D);
	            	APILib.SetActiveWindow(Client3D);
	            	
					IntPtr wParam = IntPtr.Zero;
					byte[] sarr = System.Text.Encoding.Default.GetBytes(msg);
					int len = sarr.Length;
					COPYDATASTRUCT cds;
					cds.dwData = (IntPtr)100;
					cds.lpData = msg;
					cds.cbData = len + 1;
					IntPtr result = SendMessage(Client3D, WM_COPYDATA, wParam, ref cds);
					return true;
				}
            }catch{}
			return false;
		}
		
		protected override void WndProc(ref Message m)
		{
			try{			
		        switch (m.Msg)
		        {
			        case WM_COPYDATA:
						COPYDATASTRUCT mystr = new COPYDATASTRUCT();
						Type mytype = mystr.GetType();
						mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
						string msg = mystr.lpData;
			            
						if(ReceiveWindowMessageEvent!=null)
							ReceiveWindowMessageEvent(msg);
			            
			            break;
		        }
		        base.WndProc(ref m);
			}catch{}
	    }
		#endregion
	}
}
